package git.soulbgm.sync.config;

import git.soulbgm.sync.interceptor.SqlInterceptor;
import git.soulbgm.sync.mapper.SqlMapper;
import git.soulbgm.sync.properties.DBSyncProperties;
import git.soulbgm.sync.task.SyncDataBaseTask;
import git.soulbgm.sync.web.DBSyncController;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.client.RestTemplate;

/**
 * 数据库同步自动配置类
 *
 * @author SoulBGM
 * @date 2022/04/24 14:36
 */
@EnableAsync
@Configuration
@MapperScan(basePackageClasses = SqlMapper.class)
@EnableConfigurationProperties({DBSyncProperties.class})
public class DBSyncAutoConfiguration {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    public SyncDataBaseTask syncDataBaseTask(DBSyncProperties properties, RestTemplate restTemplate) {
        return new SyncDataBaseTask(properties, restTemplate);
    }

    @Bean
    public SqlInterceptor sqlInterceptor(SyncDataBaseTask task) {
        return new SqlInterceptor(task);
    }

    @Bean
    public DBSyncController dbSyncController(SqlMapper mapper) {
        return new DBSyncController(mapper);
    }

}
